" Vim filetype plugin file
" Language:     Ch
" Maintainer:   SoftIntegration, Inc. <info@softintegration.com>
" URL:		http://www.softintegration.com/download/vim/ftplugin/ch.vim
" Last change:	2004 May 16
"		Created based on cpp.vim
"
" Ch is a C/C++ interpreter with many high level extensions
"

" Only do this when not done yet for this buffer
if exists("b:did_ftplugin")
  finish
endif


" Behaves just like C
runtime! ftplugin/c.vim ftplugin/c_*.vim ftplugin/c/*.vim

function _q() range
	echo expand('%:p')
endfunction

function _cc(filename) range
	let lnum = a:firstline
	let n = 0
	echo "firstline : " . lnum . " , filename : " . a:filename
	while lnum <= a:lastline
		let n = n + len(split(getline(lnum)))
		let lnum = lnum + 1
	endwhile
	echo "found " . n . " words"
endfunction

function _ss(filename) range
	let search_desc = ""
	let line = getline(a:firstline)
	let slist =  matchlist(line,'\(ITERATE\)\s\+%\(\S\+\)')
	let slen =  len(slist)
	"echo slen
	let plist =  matchlist(line,'+<+\s*\$\(\S\+\)\s*+>+')
	let plen = len(plist)
	echo "CURRENT line: " . line . " , slen: " . slen . ", plen: " . plen
	echo expand('%:p')
	echo slist
	echo plist
	let input = ""
	let is_s = 1  	" is_s=1 : ITERATE type , is_s=0 : Value type
	
	if(slen > 1)
		let search_desc = "GET ITERATE HASH %"
		let input = slist[2]
		let is_s= 1
	elseif(plen > 1)
		let search_desc = "GET VALUE +<+ .. +>+"
		let input = plist[1]
		let is_s= 0
	else
		return
	endif

	let ssp = []
	let temp_ssp = split(input,'{')
	let ssp = copy(temp_ssp)
	let lnum = 0
	for c in ssp
		if(lnum > 0)
			let ssp[lnum] = '{'.c
		else
			let ssp[lnum] = '$'.c
		endif
		let lnum = lnum +1
	endfor

	echo ssp
	let ssplen = len(ssp)
	"echo ssplen

	let n = 0
	let gline = ""
	let gsp = []
	let gdict = {}
	for gline in readfile(a:filename,'')
		let n = n+1
		let cop_prefix = a:filename . ":" . n . ":"
		"echo gline
		let temp_glist =  split(gline,'=')
		"echo temp_glist
		let lll = len(temp_glist)
		"echo lll
		if(lll <= 1)		" = is not exist
			continue
		endif
		let plist =  matchlist(temp_glist[0],'\(\$.*\)')
		let temp_gsp = split(plist[1],'{')
		let gsp = copy(temp_gsp)
		let glen = len(gsp)
		"echo glen
		let lnum = 0
		for c in gsp
			if(lnum > 0)
				let gsp[lnum] = '{'.c
			endif
			let lnum = lnum +1
		endfor
		"echo gsp
		"echo ssp
		"echo "ssplen=" . ssplen . ", glen=" . glen
		if(is_s == 1)
			if(ssplen >= glen) 
				continue
			endif
		else
			if(ssplen != glen) 
				continue
			endif

		endif

		let lnum = 0
		let itflag = 0		" 0 : all match  , >0 : not match
		for c in ssp
			"echo "ITKEY"
			"echo c
			let lk = match(c,'IT\d\?KEY')
			let lv = match(c,'IT\d\?VALUE')
			let no_mf = 1
			if(lk < 0) 
				if (lv < 0) 		"  no match (IT?KEY , IT?VALUE)
					let no_mf = 0		" NO Match
				endif
			endif
			if(no_mf == 0)	" no match (IT?KEY , IT?VALUE)
				"echo "NO MATCH - ITKEY"
				"echo lnum
				let gl =  gsp[lnum]
				"echo gl
				"echo c
				let lit = match(gl , c)
				if(lit < 0)
					let itflag = itflag +1
				endif
			endif
			let lnum = lnum +1
		endfor
		"echo "DDD itflag=" . itflag . ", is_s=" . is_s
		if(itflag ==0)
			if(is_s == 1)
				"echo "Match $..{..}{..}"
				"echo lnum
				"echo gsp[lnum]
				"echo gline
				"echo gsp
				let templnum = lnum + 1
				unlet gsp[templnum : ]
				"echo gsp
				let gspline = join(gsp," ")
				"call extend(gdict,{gsp[lnum]: gline})
				"call extend(gdict,{gspline : gline})
				call extend(gdict,{gspline : cop_prefix})
				"echo gdict
			else
				"echo "KK1"
				"echo gsp
				let gspline = join(gsp," ")
				"echo gspline
				"echo "KK2"
				call extend(gdict,{gspline : gline})
			endif
		endif
	endfor
	let cnt = 0
	let search = join(ssp," ")
	echo "==============RESULT================="
	echo "Type Description : " . search_desc
	echo "Original Line : " . line
	echo "Searching Item : " . search
	let answer_line =  expand('%:p') . "|" . a:firstline . "| Answer of Question"
	let answer_list = [answer_line , ]
	for [key,value] in items(gdict)
		"echo '[' . cnt . '] KEY= ' . key . '::::: VALUE= ' . value
		"let t_str = '[' . cnt . '] KEY= '. key . '::::: VALUE= ' . value
		echo value . '[' . cnt . '] ' . key 
		let t_str = value . '[' . cnt . '] ' . key 
		call add(answer_list, t_str)
		let cnt = cnt + 1 
	endfor
	let gpid_str = "/tmp/cga-vim." . getpid()
	echo "getpid : " . gpid_str
	let tm_str = ".cga-vim.val" 
	call writefile(answer_list,tm_str)
	echo "getpid : " . gpid_str
	" you can show this context after :cfile gpid_str  and :copen
endfunction
